$(function () {
    layui.use(['form','config'], function () {
        var layer = layui.layer,
            $ = layui.jquery,
            config = layui.config;

        window.frontLogin = function() {
            let params = {};
            params.username=$("#username").val();
            params.password=$("#pwd").val();
            params.captcha=$("#verifycode").val();
            let result = sendPost("api-auth/oauth/token",JSON.stringify(params));
            if(result.code==0){
                config.putToken(data);
                layer.msg('登录成功', {time: 1000}, function () {
                    window.location = './index.html';
                });
            }else {
                layer.msg(result.msg);
            }
        }
        window.register = function () {
            let password = $("#password").val();
            let repassword = $("#re-password").val();
            let params = {};
            params.username = $("#username").val();
            params.password = password;
            params.phone = $("#phone").val();
            params.email = $("#email").val();
            if ($("#username").val() == '') {
                mesg('请输入您的账号');
                return false;
            } else if(password==''){
                mesg('请输入您的密码');
                return false;
            }else if(repassword==''){
                mesg('请再次输入密码');
                return false;
            }else if($("#phone").val()==''){
                mesg('请输入您的电话');
                return false;
            }else if($("#email").val()==''){
                mesg('请输入您的邮箱');
                return false;
            }else if(!judge()){
                mesg('电话号码格式不正确');
                return false;
            }else if(password!=repassword){
                mesg('两次密码不一致');
                return false;
            } else if(password===repassword) {
                let result = sendPost("api-user/user/register",JSON.stringify(params));
                console.log(result)
                if(result.code==0){
                    layer.msg('注册成功', {time: 500}, function () {
                        window.location.href = "login.html"
                    });
                }
            }
        }
        window.logout = function() {
            layer.confirm('确认退出登录吗', function (index) {
                layui.data('LocalData', {
                    key: "loginNameFront",
                    value: ""
                })
                layui.data('LocalData', {
                    key: "access_token_front",
                    value: null
                })
                window.location.href = "login.html"
            })
        }
        window.information = function() {
            window.location.href = "./information.html"
        }

    })
});